HDU-1241 Oil Deposits(BFS or DFS 水题)

描述

传送门:HDU-1241 Oil Deposits

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

输入描述

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either ‘*’, representing the absence of oil, or `@’, representing an oil pocket.

输出描述

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

示例

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

输出

1
2
3
4
0
1
2
2

题解

题目大意

判断有多少个‘@’的联通块,八个方向有接触就算在同一联通块内。

思路

搜索入门题,直接暴力枚举每个点,DFS、BFS都可以,把搜索到的点标记下,维护下num标记即可。

代码

BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define LL long long
const int MAXN = 110;
using namespace std;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int m, n;
char MAP[MAXN][MAXN];
typedef pair<int, int>p;
p e;
void bfs(int x, int y){
queue<p> que;
que.push(p(x, y));
MAP[x][y] = '*';
while(!que.empty()){
e = que.front();
que.pop();
for(int i = 0; i < 8; i++){
int xx = e.first+dx[i], yy = e.second+dy[i];
if(xx >= 0 && yy >= 0 && xx < n && yy < m && MAP[xx][yy] == '@'){
MAP[xx][yy] = '*';
que.push(p(xx, yy));
}
}
}
}

int main(){
while(cin >> n >> m){
if(m == 0 && n == 0) break;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >>MAP[i][j];
}
}
int cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(MAP[i][j] == '@'){
bfs(i, j);
cnt++;
}
}
}
cout << cnt << endl;
}
}
DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=100+10;
int m,n,flag[MAXN][MAXN];
char MAP[MAXN][MAXN];

void dfs(int a,int b,int rt){
if(a<0 || b<0 || a>=m || b>=n)
return;
if(flag[a][b]>0 || MAP[a][b] != '@')
return;
flag[a][b] = rt;
for(int i = -1; i <= 1; i++){
for(int j = -1;j <= 1; j++){
if(i !=0 || j != 0)
dfs(a+i, b+j, rt);
}
}
}
int main(){
while(scanf("%d%d",&m,&n)){
if(m==0&&n==0)
return 0;
for(int i=0;i<m;i++)
scanf("%s", MAP[i]);
memset(flag,0,sizeof(flag));
int sum=0;
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++){
if(flag[i][j]==0 && MAP[i][j]=='@'){
dfs(i,j,++sum);
}
}
}
printf("%d\n",sum);
}
}